<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel='stylesheet' href='../rurple.css' type='text/css'>
<title>Avoiding repetitions, again!</title>
</head>
<body>
<h2 class="title">11. Avoiding repetitions, again!</h2>
<p>Rule #3 that we saw in the previous lesson is so important that I feel I
must repeat it so as to help you remember it well.</p>
<dl>
<dt><b>Rule # 3</b></dt>
<dd>When writing computer programs, do not repeat yourself.<br>
I repeat: <b>do not repeat yourself!</b></dd>
</dl>
<!--================================================-->
<hr width="50%">
<a name="Repeat" id="Repeat"></a>
<h3 class="section">Repeat!</h3>
<p>In your latest solution to the newspaper delivery exercise, there was
probably still a fair bit of repetition. For example, the command
<tt>turn_left()</tt> probably appeared three times in a row in the definition
of <tt>turn_right()</tt>. Similarly, <tt>climb_up_one_stair()</tt> probably
appeared four times in the definition of <tt>climb_up_four_stairs()</tt>.
This seems to run contrary to our rule about not repeating ourselves. A way
to avoid such repetition is to ask Reeborg to repeat instructions through a
special command.</p>
<p>To have Reeborg repeat an instruction, we use the command
<tt>repeat()</tt> as follows:<br>
<tt>repeat(</tt> <em>name of instruction</em>, number of times<tt>)</tt></p>
<p>Note that the <em>name of instruction</em> is the name without the
parentheses () at the end. For example, we could write</p>
<pre>
<span class="keyword">def</span> turn_right():
    repeat(turn_left, 3)
</pre>
<h3 class="try">Your turn</h3>
<p>Make use of <tt>repeat()</tt> everywhere you can to write a shorter
version of the newspaper delivery challenge. Make sure that your new program
works as expected.</p>
<!--============================================-->
<hr width="50%">
<a name="Challenges2" id="Challenges2"></a>
<h3 class="suggested">Challenges</h3>
<p>We end this lesson with a few exercises. Respecting the theme of this
lesson (but not the title!), the first two exercises are repetitions of 
two exercises we had at the end of the lesson on building walls. 
To solve them, you have to make use of the
new concepts [<span class="pykeyword">def</span>, <tt>repeat()</tt>] you have
seen in this lesson and in the previous one.</p>
<h3 class="suggested">Jumping hurdles</h3>
<p>Reeborg has entered a hurdles race. Write a new program that have him
follow the path indicated below in his way to the finish line. The world file
is hurdles1.wld.</p>
<center><img alt="hurdles start" src=
"../../images/intro/hurdles1_start.png"></center>
<center><img alt="lead to" src="../../images/lead_to.png"> <img alt=
"hurdles end" src="../../images/intro/hurdles1_end.png"></center>
<p>It might be useful to define, among others, a new instruction,
<tt>jump_hurdle()</tt>, which would correspond to the following path:</p>
<center><img alt="jump one hurdle start" src=
"../../images/intro/jump1start.png"> <img alt="lead to" src=
"../../images/lead_to.png"> <img alt="jump one hurdle end" src=
"../../images/intro/jump1end.png"></center>
<p>Compare your new solution with the previous one. [You did save it, didn't
you?]</p>
<hr width="50%">
<h3 class="suggested">Harvest time</h3>
<p>It's harvest time! Have Reeborg pick up all the carrots (represented by
beepers) in the garden shown below. The world file is harvest1.wld.</p>
<p><img src="../../images/intro/harvest1.png" alt="harvest start"></p>
<p>Your program should define the following instructions:</p>
<pre>
move_to_first_row()
harvest_two_rows()
move_right_to_next_row()
</pre>
<p>You may want to break up these instructions further; for example, you
<b><i>could</i></b> have:</p>
<pre>
<span class="keyword">def</span> harvest_two_rows():
    harvest_one_row()
    move_left_to_next_row()
    harvest_one_row()
</pre>
<p>with an approppriate definition for <code>harvest_one_row()</code>.
However, you could choose your own way of breaking up the should required
instructions. With the three required instructions defined, 
my version of the program, other than the definitions, is written 
(with still a few repetitions) as follows:</p>
<pre>
move_to_first_row()
harvest_two_rows()
move_right_to_next_row()
harvest_two_rows()
move_right_to_next_row()
harvest_two_rows()
turn_off()
</pre>
<p>Again, compare your new solution with the previous one you had for the
harvesting problem.</p>
<h3 class="suggested">Harvest time again!</h3>
<p>It's harvest time again! However, this time the rows in the garden have
been set diagonally. Have Reeborg pick up all the carrots (represented by
beepers) in the garden shown below. The world file is harvest4.wld.</p>
<p><img src="../../images/intro/harvest2.png" alt="harvest start"></p>
<p>As in the previous example, your program could define the following
instructions:</p>
<pre>
move_to_first_row()
harvest_two_rows()
move_right_to_next_row()
</pre>
<p>These instructions will <b><u>not</u></b> be defined the same way as
before. However, once you have these instructions defined, your program
should be written in the same way as the previous example:</p>
<pre>
move_to_first_row()
harvest_two_rows()
move_right_to_next_row()
harvest_two_rows()
move_right_to_next_row()
harvest_two_rows()
turn_off()
</pre>
<p>Careful not to hit a wall! Think of where you want to start harvesting,
and in which direction you are going to harvest. It may help to sketch a path
on a piece of paper.</p>
<center><a href="10-def.htm"><img alt="previous" src=
"../../images/previous.png"> Definitely avoiding repetitions</a> - <a href=
"../lessons_toc.htm"><img alt="home" src="../../images/home.png"></a> - <a href=
"12-if.htm">If only Reeborg could decide on his own ... <img alt="next" src=
"../../images/next.png"></a></center>
</body>
</html>
